package cn.memedai.router.demo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Map;
import cn.memedai.router.RouteCallback;
import cn.memedai.router.RouteResult;
import cn.memedai.router.RouteTable;
import cn.memedai.router.Router;

public class MainActivity extends AppCompatActivity {

    private EditText pathEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pathEdit = findViewById(R.id.edit_route);
        // 动态添加路由
        Router.addRouteTable(new RouteTable() {
            @Override
            public void handleActivityTable(Map<String, Class<? extends Activity>> map) {
                map.put("dynamic", DynamicActivity.class);
            }
        });
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                Router.build(pathEdit.getText().toString().trim()).callback(new RouteCallback() { // 添加结果回调
                    @Override
                    public void callback(RouteResult state, Uri uri, String message) {
                        if (state == RouteResult.SUCCEED) {
                            Toast.makeText(MainActivity.this, "succeed: " + uri.toString(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "error: " + uri + ", " + message, Toast.LENGTH_SHORT).show();
                        }
                    }
                }).go(this);
                break;
            case R.id.btn2:
                Router.build("dynamic").go(this);
                break;
            case R.id.btn3:
                Bundle bundle = new Bundle();
                bundle.putString("extra", "Bundle from MainActivity.");
                Router.build("ForResultActivity").requestCode(0).extras(bundle).go(this);
                break;
            case R.id.btn4:
                Router.build("TestActivity").anim(R.anim.slide_in, R.anim.slide_out).go(this);
                break;
            case R.id.btn5:
                Router.build(Uri.parse("router://custom_filter?id=9527&status=success")).go(this);
                break;
            case R.id.btn6:
                Router.build("ModuleActivity").go(this);
                break;
            case R.id.btn7:
                Router.build("InterceptedActivity").go(this);
                break;
            case R.id.btn8:
                Router.build("zhoupu://open?page=Test&id=3&name=ok") .anim(R.anim.slide_in, R.anim.slide_out).go(this);
                break;
            case R.id.btn9:
                Router.build("https://www.zhoupu123.com/saaswebapp/#/attendanceStatistics") .anim(R.anim.slide_in, R.anim.slide_out).go(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == RESULT_OK) {
            String result = data.getStringExtra("extra");
            Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        }
    }
}
